package com.utonw.carloan.activity.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.utonw.carloan.R;
import com.utonw.carloan.app.CrashApplication;
import com.utonw.carloan.app.MyApplication;
import com.utonw.carloan.global.ReleaseData;
import com.utonw.carloan.utils.UIUtils;
import com.utonw.carloan.utils.ViewUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by SEELE on 2017/3/20.
 */

public abstract class BaseActivity extends AppCompatActivity {
    /**
     * ToolBar
     */
    protected Toolbar mTbHeadBar;
    /**
     * ToolBar主题
     */
    protected TextView mMainTitle;
    /**
     * ToolBar左侧HomeButton
     */
    protected ActionBarDrawerToggle mToggle;
    /**
     * ToolBar右侧小人图标
     */
    protected LinearLayout mConfig;
    /**
     * 是否显示右侧图标
     */
    protected boolean isShowConfig;
    /**
     * 侧滑菜单
     */
    protected DrawerLayout mMyDrawable;
    /**
     * 侧滑菜单中的ListView
     */
    protected ListView mLvMenu;

    //测试用抽屉数据
    private List<String> lvMenuList = new ArrayList<String>() {{
        add("11111111");
        add("22222222");
        add("33333333");
        add("44444444");
    }};
    /**
     * 记录前台 Activity
     */
    public static BaseActivity sForegroundActivity;
    /**
     * 记录所有没有销毁的 Activity
     */
    public LinkedList<BaseActivity> mActivities = new LinkedList<>();

    /**
     * 获取前台 Activity
     */
    public static BaseActivity getForegroundActivity() {
        return sForegroundActivity;
    }

    /**
     * 校验View集合
     */
    private LinkedHashMap<Integer, View> checkViews;

    /**
     * 启动一个 Activity
     *
     * @param intent
     */
    public static void startAnActivity(Intent intent) {
        if (sForegroundActivity != null) {
            sForegroundActivity.startActivity(intent);
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            MyApplication.getContext().startActivity(intent);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_base);
        //设置判断集合
        checkViews = new LinkedHashMap<>();

        ReleaseData.tempUnused.add(checkViews);

        //部署页面
        ViewUtils.initContentView(this, R.id.fl_main_content, setLayout());
        initView();
        initData();
        initDrawerLayout();
        initActionBar();
        initTitle();
        mActivities.add(this);
        try {
            ((CrashApplication) getApplicationContext()).addActivity(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onResume() {
        sForegroundActivity = this;
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (sForegroundActivity == this) {
            sForegroundActivity = null;
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mActivities.remove(this);
        super.onDestroy();
    }

    //设置布局
    public abstract int setLayout();

    //初始化组件
    public abstract void initView();

    //初始化数据的方法
    public abstract void initData();

    //设置标题文字
    public abstract String setTitle();

    public void initActionBar() {
        mTbHeadBar = (Toolbar) findViewById(R.id.tbHeadBar);
        mConfig = (LinearLayout) findViewById(R.id.ll_person);
        setSupportActionBar(mTbHeadBar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setHomeAsUpIndicator(R.mipmap.nav_narrow);

        mTbHeadBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        mConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.showShortToast("这是一个小人图标~");
            }
        });

        if (!isShowConfig) {
            mConfig.setVisibility(View.INVISIBLE);
        }

    }

    public void initDrawerLayout() {
        mLvMenu = (ListView) findViewById(R.id.lvMenu);
        mMyDrawable = (DrawerLayout) findViewById(R.id.dlMenu);
        //测试数据，可以替换
        mLvMenu.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, lvMenuList));
        mLvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mMyDrawable.closeDrawer(Gravity.LEFT);
            }
        });

    }


    public void initTitle() {
        mMainTitle = (TextView) findViewById(R.id.tv_main_title);
        mMainTitle.setText(setTitle());
    }


    @Override
    public void onBackPressed() {
        if (mMyDrawable.isDrawerOpen(Gravity.LEFT)) {
            mMyDrawable.closeDrawers();
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 结束所有没有销毁的 Activity, 结束当前进程
     */
    public void killAll() {
        List<BaseActivity> copy;
        synchronized (mActivities) {
            copy = new LinkedList<>(mActivities);
        }
        for (BaseActivity activity : copy) {
            activity.finish();
        }
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public LinkedHashMap<Integer, View> getCheckViews() {
        return checkViews;
    }


    public void setCheckViews(int stringId, View view) {

        getCheckViews().put(stringId, view);

    }


    //校验数据集合
    public boolean checkEmpty() {
        boolean MFlag = true;
        for (Object o : getCheckViews().keySet()) {

            int key = (int) o;
            View viewToCheck = getCheckViews().get(key);
            try {
                if (TextUtils.isEmpty(((EditText) viewToCheck).getText())) {
                    MFlag = false;
                    UIUtils.showShortToast(UIUtils.getString(key) + " 是必填项");
                    break;
                }

            } catch (Throwable e) {
                if (((TextView) viewToCheck).getText().
                        equals(getString(R.string.apply_page_begin_please_choose))) {
                    MFlag = false;
                    UIUtils.showShortToast(UIUtils.getString(key) + " 是必选项");
                    break;
                }
            }
        }

        return MFlag;
    }

}
